﻿Imports System.ComponentModel
Imports System.Threading
Imports System.Collections.ObjectModel
Imports System.Text
Imports iConverter.Models
Imports iConverter.Core
Imports iConverter.Commands

Namespace ViewModels

    Partial Class PluginSettingsViewModel


        ''' <summary>
        ''' 注册窗口事件
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub RegisterWindowsEvent()
            AddHandler Me._win.Closing, AddressOf Window_Closing
        End Sub


        ''' <summary>
        ''' 卸载窗口事件
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub UnRegisterWindowsEvent()
            RemoveHandler Me._win.Closing, AddressOf Window_Closing

        End Sub

#Region " 窗口关闭事件"

        Private Sub Window_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)




            If Me.IsChanged AndAlso ShowCloseQuestionMessage() = MessageBoxResult.No Then
                e.Cancel = True
            Else
                e.Cancel = False

              '  UnRegisterWindowsEvent()
                Me.Close(True)
            End If

        End Sub

#End Region


        ''' <summary>
        ''' 注册 Command Bindings
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub RegisterCommandBindings()


            Me._win.CommandBindings.Add(New CommandBinding(PluginSettingsCommands.OKCommand, AddressOf OnExecuteOK, AddressOf OnCanExecuteOK))
            Me._win.CommandBindings.Add(New CommandBinding(PluginSettingsCommands.CancelCommand, AddressOf OnExecuteCancel, AddressOf OnCanExecuteCancel))
            Me._win.CommandBindings.Add(New CommandBinding(PluginSettingsCommands.ResetCommand , AddressOf OnExecuteReset , AddressOf OnCanExecuteReset ))

        End Sub






#Region "Reset 命令"

        Private Sub OnCanExecuteReset(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
            e.CanExecute = True
            e.Handled = True
        End Sub

        Private Sub OnExecuteReset(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
            'TODO:修改字符串到资源
            Dim r = MessageBox.Show("你确定要恢复插件设置到默认值吗？", "重置", MessageBoxButton.YesNo, MessageBoxImage.Question)

            If r=MessageBoxResult.Yes then 
                Me.CurrentSettings.Reset

                Me.Configurator.Unload(Me.CurrentSettings)

                Me.Close()
            End If

            e.Handled = True
        End Sub

#End Region





#Region "OK 命令"

        Private Sub OnCanExecuteOK(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
            e.CanExecute = True
            e.Handled = True
        End Sub


        Private Sub OnExecuteOK(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)

            ' 更新设置，并保存

            Me.CurrentSettings.Save()

            Me.Configurator.Unload(Me.CurrentSettings)
            Me.Close()

            e.Handled = True
        End Sub

#End Region





#Region "Cancel 命令"

        Private Sub OnCanExecuteCancel(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
            e.CanExecute = True
            e.Handled = True
        End Sub

        Private Sub OnExecuteCancel(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)

            '取消更新，关闭窗口
            Me.CurrentSettings.Reload()
            Me.Configurator.Unload(Me.CurrentSettings)

            Me.Close()

            e.Handled = True
        End Sub

        ''' <summary>
        ''' '显示关闭窗口消息
        ''' </summary>
        ''' <returns>MessageBoxResult.No  OR MessageBoxResult.Yes OR MessageBoxResult.None</returns>
        ''' <remarks>
        ''' 如果是用户主动放弃修改，将不会提示问题并返回 None结果
        ''' </remarks>
        Private Function ShowCloseQuestionMessage() As System.Windows.MessageBoxResult
            If Me._isAskClosing Then
                Return MessageBox.Show("程序检测到插件设置更改，如果继续将会丢失更改。是否要放弃修改？", "设置更改", MessageBoxButton.YesNo, MessageBoxImage.Question)
            Else
                Return MessageBoxResult.None
            End If
        End Function
#End Region





    End Class

End Namespace


